package com.kiln.zhongjingst.wxapi

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import client.comm.baoding.ui.GoodsOrderActivity
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.kiln.zhongjingst.wxapi.WXEntryActivity
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelmsg.ShowMessageFromWX
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram
import com.tencent.mm.opensdk.utils.Log
import java.lang.Exception

class WXEntryActivity : Activity(), IWXAPIEventHandler {
    private lateinit var api: IWXAPI
    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        api = WXAPIFactory.createWXAPI(this, "wx45b9026a73a18104", false)
        try {
            val intent = intent
            api.handleIntent(intent, this)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        setIntent(intent)
        api!!.handleIntent(intent, this)
    }

    override fun onReq(baseReq: BaseReq) {
        Log.d(TAG, "onReq: " + baseReq.type)
        when (baseReq.type) {
            ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX -> {}
            ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX -> goToShowMsg(baseReq as ShowMessageFromWX.Req)
            else -> {}
        }
        finish()
    }

    private fun goToShowMsg(baseReq: ShowMessageFromWX.Req) {
        Log.d(TAG, "goToShowMsg:" + baseReq.message)
    }

    override fun onResp(resp: BaseResp) {
        Log.d(TAG, "onResp: " + resp.type)
        if (resp.type == ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM) {
            val launchMiniProResp = resp as WXLaunchMiniProgram.Resp
            val extraData = launchMiniProResp.extMsg //对应小程序组件 <button open-type="launchApp"> 中的 app-parameter 属性
            //extraData: 0000支付成功，9999支付失败
            Log.d("DDD", extraData)

            when (extraData) {
                "0000" -> {
                    startActivity(Intent(this, GoodsOrderActivity::class.java).apply {
                        putExtra("index", 2)
                        putExtra("msg", "支付成功")
                    })
                    sendBroadcast(Intent("ccsdgsdgs"))
                    finish()
                }
                else -> {
                    startActivity(Intent(this, GoodsOrderActivity::class.java).apply {
                        putExtra("index", 1)
                        putExtra("msg", "支付失败")
                    })
                    sendBroadcast(Intent("ccsdgsdgs"))

                    finish()
                }
            }
        }
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy")
    }

    companion object {
        private val TAG = WXEntryActivity::class.java.simpleName
    }
}